#include <cstdio>
#include <cstring>

#define MAXN 200+5
#define INF 0x3F3F3F3F
#define MIN(a,b) (a<b?a:b)

int N, M;
int G[MAXN][MAXN];
bool vis[MAXN];

int dfs(int s, int e, int c) {
	if (s==e) return c;
	vis[s] = true;
	for (int i=1; i<=M; i++) {
		if (!vis[i] && G[s][i]>0) {
			int d = dfs(i, e, MIN(c, G[s][i]));
			if (d>0) {
				G[s][i] -= d;
				G[i][s] += d;
				return d;
			}
		}
	}
	return 0;
}

int max_flow() {
	int flow = 0;
	while(1) {
		memset(vis, false, sizeof(vis));
		int d = dfs(1, M, INF);
		if (d == 0) return flow;
		else flow += d;
	}
}

int main() {
	while(~scanf("%d%d", &N, &M)) {
		memset(G, 0, sizeof(G));
		for (int i=1; i<=N; i++) {
			int S, E, C;
			scanf("%d%d%d", &S, &E, &C);
			G[S][E] += C;
		}
		int ans = max_flow();
		printf("%d\n", ans);
	}
	return 0;
}
